package com.xt.shop.Controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xt.shop.Model.Option;
import com.xt.shop.Model.RestBean;
import com.xt.shop.Model.ShopInfo;
import com.xt.shop.service.impl.ShopInfoServiceImpl;
import com.xt.shop.service.impl.UserInfoServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;


@RestController
@RequestMapping("/api/shop")
public class ShopInfoController {

    @Resource
    ShopInfoServiceImpl service;

    @Resource
    UserInfoServiceImpl userInfoService;

    @RequestMapping("/get/{page}/{item}")
    public RestBean queryList(@PathVariable int page, @PathVariable int item, String name) {
        return RestBean.success(service.queryList(page, item, name));
    }

    @RequestMapping("/g/{id}")
    public RestBean get(@PathVariable String  id){
        ShopInfo byId = service.getById(id);
        if (byId != null){
            byId.setUserId(userInfoService.getById(byId.getUserId()).getUsername());
            return RestBean.success(byId);
        }
        return RestBean.failure(400);
    }

    @RequestMapping("/del")
    public RestBean del(String id) {
        ShopInfo byId = service.getById(id);
        if (byId != null) {
            byId.setState(-1);
            if (service.updateById(byId)) {
                return RestBean.success();
            }
            return RestBean.failure(500, "删除失败");
        }
        return RestBean.failure(400, "参数异常");
    }

    @RequestMapping("/insert")
    public RestBean insert(@RequestBody ShopInfo shopInfo) {
        String uid = UUID.randomUUID().toString();
        shopInfo.setShopId(uid);
        shopInfo.setInputTime(new Date());
        shopInfo.setUpdateTime(new Date());
        shopInfo.setState(1);
        if (service.save(shopInfo)) {
            return RestBean.success(service.getById(uid));
        }
        return RestBean.failure(500, "保存失败");
    }

    @RequestMapping("/update")
    public RestBean update(@RequestBody ShopInfo shopInfo){
        ShopInfo byId = service.getById(shopInfo.getShopId());
        if (byId!= null){
            shopInfo.setUpdateTime(new Date());
            shopInfo.setInputTime(byId.getInputTime());
            if(service.updateById(shopInfo)){
                return RestBean.success(service.getById(shopInfo.getShopId()));
            }
            return RestBean.failure(500,"更新失败");
        }
        return RestBean.failure(400,"参数异常");
    }

    @RequestMapping("/options")
    public RestBean option(){
        List<ShopInfo> list = service.list(new QueryWrapper<ShopInfo>().orderByDesc("input_time"));
        List<Option> options=new ArrayList<>();
        for (ShopInfo shopInfo:list){
            Option o=new Option(shopInfo.getName(),shopInfo.getShopId());
            options.add(o);
        }
        return RestBean.success(options);
    }

}
